package com.enfi.api.modules.db.base;

import com.enfi.api.util.DateTimeUtils;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @author wind
 * @since 2020-04-07 15:47:33
 */
@Data
public  class BaseVO<ID> {
    @NotNull(groups = {IGroupPut.class,IGroupPatch.class,IGroupDelete.class},message = "不为空")
    protected  ID id;

    protected MatchQuery query= new MatchQuery();

    protected Map<String,Object> range= Maps.newHashMap();

    public  void init(Map<String,String[]> map){
        map.entrySet().stream().forEach(v->{
            String vv="";
            String[] values = v.getValue();
            if (values.length > 0) {
              vv=values[0];
            }
            RangeQuery rangeQuery=null;
            String c=v.getKey();
            if(v.getKey().startsWith("start")&& DateTimeUtils.isDateTime(vv)){
                c=c.substring(5);
                rangeQuery=query.getRange().get(v.getKey());
                if(rangeQuery==null){
                    rangeQuery=new RangeQuery();
                }
                rangeQuery.setGte(vv);
                query.getRange().put(StringUtils.uncapitalize(c),rangeQuery);
            }
            if(v.getKey().startsWith("end")&&DateTimeUtils.isDateTime(vv)){
                c=c.substring(3);
                rangeQuery=query.getRange().get(v.getKey());
                if(rangeQuery==null){
                    rangeQuery=new RangeQuery();
                }
                rangeQuery.setLte(vv);
                query.getRange().put(StringUtils.uncapitalize(c),rangeQuery);
            }

        });
    }

    public interface IGroupGet {
    }
    public interface IGroupGetPost {
    }
    public interface IGroupPut {
    }
    public interface IGroupPatch {
    }
    public interface IGroupPost {
    }
    public interface IGroupDelete {
    }
}